package com.cos.lession48.services.impl;

import com.cos.lession48.dao.EmpDao;
import com.cos.lession48.entity.Emp;
import com.cos.lession48.services.EmpServices;
import com.cos.lession48.util.MyBatisUtils;
import com.cos.lession48.util.StringUtil;

import java.util.List;

/**
 * @author: COS
 * @time: 2022/3/1 17:18
 * @description:
 */
public class EmpServicesImpl implements EmpServices {

    @Override
    public List<Emp> getAll() {
        List<Emp> all = null;
        try {
            //获取empDao实现类
            EmpDao mapper = MyBatisUtils.getMapper(EmpDao.class);
            all = mapper.getAll();
            MyBatisUtils.close();
        } catch (Exception e) {
            e.printStackTrace();
            MyBatisUtils.rollback();
        }
        return all;
    }

    @Override
    public Emp login(String ename, String pwd) {
        Emp e = null;
        //1 字符串判断
        try {
            if (StringUtil.isNotEmpty(ename, pwd)) {
                //2 获取EmpDao的实现类
                EmpDao ed = MyBatisUtils.getMapper(EmpDao.class);
                //3 调用dao方法
                e = ed.findEmpByEname(ename);
                //4 判断
                if (e != null && e.getPwd().equals(pwd)) {
                    MyBatisUtils.close();
                    //用户名和密码都正确
                    return e;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            MyBatisUtils.rollback();
        }
        return null;
    }
}
